<?php
/**
 * JoiGallery - image gallery component for Joostina CMS
 *
 * Backend install handler
 *
 * @version 1.0 alpha 12
 * @package JoiGallery
 * @subpackage	Installer
 * @filename install.joigallery.php
 * @author JoostinaTeam
 * @copyright (C) 2008-2009 Joostina Team
 * @license see license.txt
 *
 **/

defined('_VALID_MOS') or die();

function com_install() {

  $mainframe = &mosMainFrame::getInstance();
  $database = &database::getInstance();

  //Создание директорий
  $dirs = array('joigallery', 'joigallery/originals', 'joigallery/thumbs', 'joigallery/resized');
  foreach($dirs as $dir) {
          joi_check_dir(JPATH_BASE.'/images/'.$dir);
          joi_chmod (JPATH_BASE.'/images/'.$dir);
  }

  //РАСПАКОВКА АРХИВОВ  
  //Файлы фронта
  $zipfile = JPATH_BASE.DS.'components'.DS.'com_joigallery'.DS.'front.zip';
  $unzip_dir = JPATH_BASE.DS.'components'.DS.'com_joigallery';
  if(!joi_unzip($zipfile, $unzip_dir)) return false;
  joi_chmod ($unzip_dir);

  //Файлы админки
  $zipfile = JPATH_BASE.DS.'administrator'.DS.'components'.DS.'com_joigallery'.DS.'back.zip';
  $unzip_dir = JPATH_BASE.DS.'administrator'.DS.'components'.DS.'com_joigallery';
  if(!joi_unzip($zipfile, $unzip_dir)) return false;
  joi_chmod ($unzip_dir);
  
  //Ватермарки
  $zipfile = JPATH_BASE.DS.'components'.DS.'com_joigallery'.DS.'watermark.zip';
  $unzip_dir = JPATH_BASE.'/images/joigallery/watermark';
  if(!joi_unzip($zipfile, $unzip_dir)) return false;
  joi_chmod ($unzip_dir);

  //Копирование index.html в директории изображений
  $dirs = array('joigallery', 'joigallery/originals', 'joigallery/thumbs', 'joigallery/resized', 'joigallery/watermark');
  foreach($dirs as $dir) {
          copy (JPATH_BASE.'/administrator/components/com_joigallery/index.html',
                JPATH_BASE.'/images/'.$dir.'/index.html');
  }

  // Установка иконок меню
  $database->setQuery("UPDATE #__components
                          SET admin_menu_img='../administrator/components/com_joigallery/icons/component.png'
                        WHERE admin_menu_link='option=com_joigallery'");
  $iconresult[0] = $database->query();
  $database->setQuery("UPDATE #__components
                          SET admin_menu_img='../administrator/components/com_joigallery/icons/photos.png'
                        WHERE admin_menu_link='option=com_joigallery&act=photos'");
  $iconresult[1] = $database->query();
  $database->setQuery("UPDATE #__components
                          SET admin_menu_img='../administrator/components/com_joigallery/icons/categories.png'
                        WHERE admin_menu_link='option=com_joigallery&act=categories'");
  $iconresult[2] = $database->query();
  $database->setQuery("UPDATE #__components
                          SET admin_menu_img='../administrator/components/com_joigallery/icons/tools.png'
                        WHERE admin_menu_link='option=com_joigallery&act=tools'");
  $iconresult[3] = $database->query();
  $database->setQuery("UPDATE #__components
                          SET admin_menu_img='../administrator/components/com_joigallery/icons/configuration.png'
                        WHERE admin_menu_link='option=com_joigallery&act=configuration'");
  $iconresult[4] = $database->query();
  $database->setQuery("UPDATE #__components
                          SET admin_menu_img='../administrator/components/com_joigallery/icons/about.png'
                        WHERE admin_menu_link='option=com_joigallery&act=about'");
  $iconresult[5] = $database->query();


  if(!$database->query()) echo $database->getErrorMsg().'<br />';

  foreach ($iconresult as $i=>$icresult) {
          if (!$icresult) echo '<font color="red">Ошибка установки: </font> иконка '.$i
                              .' не была скопирована<br />';

  }//foreach

  return '<div style="width:100%; text-align:center">
           <div style="width:400px; margin:0px auto;">
            <img src="'.JPATH_SITE.'/administrator/components/com_joigallery/icons/logo.png"
                 style="float:left"
                 width="400" height="279" alt="" 
                 border="0" />
           </div>
          </div>';

}//function com_install

function joi_check_dir($dir) {

  if (!is_dir($dir)) {
          umask(011);
          if (!mkdir($dir)) {
                  echo 'Ошибка установки: невозможно создать директорию '.$dir;
                  return false;
          }
          mosChmod($dir, 0777);
          return true;
  }
  return true;
}

function joi_chmod ($dir) {
  $filemode = 0644;
  $dirmode  = 0755;
  mosChmodRecursive($dir, $filemode, $dirmode);
  return true;
}

function joi_unzip($zip, $unzip_dir) {
  $mainframe = &mosMainFrame::getInstance();

  require_once (JPATH_BASE.'/administrator/includes/pcl/pclzip.lib.php');
  require_once (JPATH_BASE.'/administrator/includes/pcl/pclerror.lib.php');

  $zipfile = new PclZip($zip);

  $ret = $zipfile->extract(PCLZIP_OPT_PATH, $unzip_dir);
  if ($ret == 0) {
          echo "Неисправимая ошибка: " . $zipfile->errorName(true).'<br />';
          return false;
  }
  unlink($zip);
  return true;
}
?>